import { _decorator, Component, Vec3, input, Input, EventMouse, Animation, SkeletalAnimation } from 'cc';//从cc中引入相关的模块
const { ccclass, property } = _decorator;//从_decorator中引入相关注解

@ccclass('PlayerController')//表示定义一个cocos的类，继承Component父类
export class PlayerController extends Component {

    // 是否接收到跳跃指令
    private _startJump: boolean = false;
    // 跳跃步长
    private _jumpStep: number = 0;
    // 当前跳跃时间
    private _curJumpTime: number = 0;
    // 每次跳跃时长
    private _jumpTime: number = 0.3;
    // 当前跳跃速度
    private _curJumpSpeed: number = 0;
    // 当前角色位置
    private _curPos: Vec3 = new Vec3();
    // 每次跳跃过程中，当前帧移动位置差
    private _deltaPos: Vec3 = new Vec3(0, 0, 0);
    // 角色目标位置
    private _targetPos: Vec3 = new Vec3();

    // @property({type: Animation})//定义了body移动动画，类型为Animation
    // public BodyAnim: Animation | null = null;//在cocos中与BodyAnim相关联

    @property({type: SkeletalAnimation})
    public CocosAnim: SkeletalAnimation|null = null;

    start () {
        // Your initialization goes here.
        // input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
    }

    //是否允许用户操作人物移动指令
    setInputActive(active: boolean) {
        if (active) {//注册事件回调，点击人物可以移动
            input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        } else {//取消注册事件回调，禁止点击人物移动
            input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        }
    }
    private _curMoveIndex = 0;
    //重置当前位置为0
    reset() {
        this._curMoveIndex = 0;
    }
    onMouseUp(event: EventMouse) {
        if (event.getButton() === 0) {//鼠标左键点击
            this.jumpByStep(1);//跳跃1步
        }
        else if (event.getButton() === 2) {//鼠标右键点击
            this.jumpByStep(2);//跳跃2步
        }

    }

    //跳跃
    jumpByStep(step: number) {
        if (this._startJump) {//为了避免连续点击，每次跳跃将_startJump置为true，结束后置为false
            return;
        }
        this._startJump = true;//已接收到跳跃指令
        this._jumpStep = step;//跳跃步数为step步
        this._curJumpTime = 0;//当前跳跃时间为0
        this._curJumpSpeed = this._jumpStep / this._jumpTime;//跳跃速度为跳跃步数/跳跃时间
        this.node.getPosition(this._curPos);//node为Component的属性，此处获取node的位置
        Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep, 0, 0));

        // if (this.BodyAnim) {//判断是否有动画属性
        //     if (step === 1) {//如果跳跃1步，则播放oneStep动画
        //         this.BodyAnim.play('oneStep');
        //     } else if (step === 2) {//如果跳跃2步，则播放twoStep动画
        //         this.BodyAnim.play('twoStep');
        //     }
        // }

        if (this.CocosAnim) {
            this.CocosAnim.getState('cocos_anim_jump').speed = 1.5; // 跳跃动画时间比较长，这里加速播放
            this.CocosAnim.play('cocos_anim_jump'); // 播放跳跃动画
        }

        this._curMoveIndex += step;//当前位置向前移动step格
        // this.onOnceJumpEnd();//发送事件，告知当前位置
    }

    //每次跳跃结束，发送当前的位置，事件名为JumpEnd，在GameManager中会接收
    onOnceJumpEnd() {
        if (this.CocosAnim) {
            this.CocosAnim.play('cocos_anim_idle');
        }

        this.node.emit('JumpEnd', this._curMoveIndex);
    }

    //每隔deltaTime更新帧
    update (deltaTime: number) {
        if (this._startJump) {
            this._curJumpTime += deltaTime;//当前跳跃时间加上每次相差事件
            if (this._curJumpTime > this._jumpTime) {
                // end
                this.node.setPosition(this._targetPos);
                this._startJump = false;
                this.onOnceJumpEnd();
            } else {
                // tween
                this.node.getPosition(this._curPos);
                this._deltaPos.x = this._curJumpSpeed * deltaTime;
                Vec3.add(this._curPos, this._curPos, this._deltaPos);
                this.node.setPosition(this._curPos);
            }
        }
    }
}


